package com.cwm.test.flyweight.general;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂
 */
public class FlyWeightFactory {

    private static Map<String, IFlyWeight> pool = new HashMap<String, IFlyWeight>();

    /**
     * 因为内部状态具备不变性，因此作为缓存的键
     *
     * @param intrinsicState
     * @return
     */
    public static IFlyWeight getFlyWeight(String intrinsicState) {
        if (!pool.containsKey(intrinsicState)) {
            IFlyWeight flyWeight = new ConcreteFlyWeight(intrinsicState);
            pool.put(intrinsicState, flyWeight);
        }
        return pool.get(intrinsicState);
    }
}
